//
//  DJViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/30.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DJViewController.h"
#import "GXNetTool.h"
#import "DJRootModel.h"
#import "Dj_Info.h"
#import "Banners.h"
#import "DJLocationRoot.h"
#import "CFMacro.h"
#import "DJBannerTableViewCell.h"
#import "DJTableViewCell.h"
#import "DJDetailViewController.h"

#define kTableViewCellReuse @"djTableViewCellReuse"
#define kBannderReuse @"bannerReuse"

@interface DJViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) DJLocationRoot *djLocationModel;
@property (nonatomic, strong) DJRootModel *root;

@end

@implementation DJViewController

- (IBAction)locationButtonClicked:(id)sender {
    UIButton *button = sender;
    button.selected = !button.selected;
    if (button.selected) {
        NSLog(@"点击");
    } else {
        NSLog(@"收回");
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self handleData];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"DJTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellReuse];
    [_tableView registerNib:[UINib nibWithNibName:@"DJBannerTableViewCell" bundle:nil] forCellReuseIdentifier:kBannderReuse];
}

- (void)handleData {
    // 主播页内容
    [GXNetTool GET:kDJURL(0, 1, 0) andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
        // 转数据模型
        DJRootModel *djModel = [DJRootModel modelWithDictionary:result];
        _root = djModel;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
    
    // 主播页地区
    [GXNetTool GET:kDJLocationURL andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
        DJLocationRoot *djLocation = [DJLocationRoot modelWithDictionary:result];
        _djLocationModel = djLocation;
    } andFailureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_root) {
        return _root.dj_info.count;
    } else {
        return 10;
    }
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.dj_info[indexPath.row].banners) {
        DJBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kBannderReuse];
        cell.banners = _root.dj_info[indexPath.row].banners;
        return cell;
    } else {
        DJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
        cell.info = _root.dj_info[indexPath.row];
        return cell;
    }
}

#pragma mark - row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.dj_info[indexPath.row].banners) {
        return 120;
    } else if (indexPath.row == 0) {
        return 120;
    } else {
        return 80;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DJDetailViewController *detailVC = [[DJDetailViewController alloc] init];
    detailVC.dj_info = _root.dj_info[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
